// pages/shop_staff/index.js
import {navTo,navToTab,onInput,onClick,showText} from '../../utils/reuse'
import{getVipList,addVip} from '../../api/index'
import Dialog from '../../miniprogram_npm/@vant/weapp/dialog/dialog';
Page({

  /**
   * 页面的初始数据
   */
  
  data: {
    // userInfo:{}
    List:[],
    page:1,
    pagesize:20,
    flag:true,
    page_text:'加载中...'
  },
  getVipListFn(){
    if(this.data.flag){
      var obj = {
        uid:wx.getStorageSync('uid'),
        shop_id:wx.getStorageSync('shop_id'),
        page:this.data.page,
        pagesize:this.data.pagesize
      }
      getVipList(obj).then(res=>{
        if(res.msg.length <= 0){
          return this.setData({
            flag:false,
            page_text:'加载完成！'
          })
        }
        if(res.sta == 1){
          if(res.msg.length < this.data.pagesize){
            this.setData({
              flag:false,
              page_text:'加载完成！'
            })
          }
          this.setData({
            List:[...this.data.List,...res.msg]
          })
        }else{
          if(res.msg.length <= 0){
            return this.setData({
              flag:false,
              page_text:'加载完成！'
            })
          }else{
            showText('获取数据失败！',2)
          }
          this.setData({
            flag:false
          })
        }
      })
    }

  },
  sysFn(){
    wx.scanCode({
      success(res){
        console.log(res)
        var obj = JSON.parse(res.result)
        if(new Date().getTime() - obj.current_time < 3600000){
          wx.setStorageSync('userId', obj.uid)
          wx.navigateTo({
            url: '/pages/shop_panel/index?id=' + 2,
          })
        }else{
          showText('二维码已过期！',2)
        }
      },
      fail(res){
        showText('扫码失败！',2)
      }
    })
  },
  Adminclick(e){
    Dialog.confirm({
      title: '确定要删除吗？'
    })
    .then(() => {
      const id = e.currentTarget.dataset.id
      var obj = {
        uid:wx.getStorageSync('uid'),
        shop_id:wx.getStorageSync('shop_id'),
        xuid:id
      }
      addVip(obj).then(res=>{
        if(res.sta == 1){
          this.setData({
            List:[],
            page:1,
            pagesize:10,
            flag:true,
            page_text:'加载中...'
          })
          this.getVipListFn()
          showText('删除成功！',2)
        }
      })
    })
    .catch((res) => {
      console.log(res)
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // this.getVipListFn()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      List:[],
      page:1,
      pagesize:10,
      flag:true,
      page_text:'加载中...'
    })
    this.getVipListFn()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      page:1,
      pagesize:10,
      List:[],
      flag:true
    })
    this.getVipListFn()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.setData({
      page:this.data.page + 1,
    })
    this.getVipListFn()

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})